package com.example.administrator.pomelostreet.mine;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.pomelostreet.R;

/**
 * Created by Administrator on 2017/2/10.
 */

public class MAdapter extends RecyclerView.Adapter<MAdapter.MyViewHolder> {
    public static final int TYPE_HEADER = 0;  //说明是带有Header的
    public static final int TYPE_NORMAL = 2;  //说明是不带有header的
    public OnItemClickListener listener;
    public interface OnItemClickListener {
        void onItemClick(View view,int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
    //获取从Activity中传递过来每个item的数据集合
    private Datas datas;
    private YouBiGou youbigou;
    //HeaderView, FooterView
    private View mHeaderView;
    private View mFooterView;
    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView ivYoubigou;
        private TextView tvName,tvPrice,tvShengyu;
        MyViewHolder(View itemView) {
            super(itemView);
            ivYoubigou = (ImageView) itemView.findViewById(R.id.iv_youbigou);
            tvPrice= (TextView) itemView.findViewById(R.id.price);
            tvName= (TextView) itemView.findViewById(R.id.name);
            tvShengyu= (TextView) itemView.findViewById(R.id.shengyu);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            if (listener != null) {
                listener.onItemClick(v,position);
            }
        }
    }

    public MAdapter(Datas datas, YouBiGou youBiGou){
        this.youbigou=youBiGou;
        this.datas = datas;
    }

    //HeaderView的get和set函数
    public View getHeaderView() {
        return mHeaderView;
    }
    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }
    public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(getItemCount()-1);
    }

    /** 通过判断item的类型，从而绑定不同的view    * */
    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null && mFooterView == null){
            return TYPE_NORMAL;
        }
        if (position == 0){
            //第一个item应该加载Header
            return TYPE_HEADER;
        }
        return TYPE_NORMAL;
    }

    //创建View，如果是HeaderView，直接在Holder中返回
    @Override
    public MAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mHeaderView != null && viewType == TYPE_HEADER) {
            return new MyViewHolder(mHeaderView);
        }
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.youbigou_item, parent, false);
        return new MyViewHolder(layout);
    }

    //绑定View，这里是根据返回的这个position的类型，从而进行绑定的，   HeaderView, 就不同绑定了
    @Override
    public void onBindViewHolder(MAdapter.MyViewHolder holder, int position) {
        if(getItemViewType(position) == TYPE_NORMAL){
            if(holder instanceof MyViewHolder) {
                holder.tvName.setText(datas.getItemses().get(position-1).getName());
                holder.tvShengyu.setText(datas.getItemses().get(position-1).getSttag_text()+"");
                holder.tvPrice.setText(datas.getItemses().get(position-1).getOriginal_price()+"");
                Log.i("==========", "onBindViewHolder: "+datas.getItemses().get(position-1).getPicture());
                Glide.with(youbigou).load(datas.getItemses().get(position-1).getPicture()).into(holder.ivYoubigou);
                return;
            }
            return;
        }else if(getItemViewType(position) == TYPE_HEADER){
            return;
        }else{
            return;
        }
    }

    //在这里面加载ListView中的每个item的布局
    class ListHolder extends RecyclerView.ViewHolder{
        TextView tv;
        public ListHolder(View itemView) {
            super(itemView);
            //如果是headerview或者是footerview,直接返回
            if (itemView == mHeaderView){
                return;
            }
            if (itemView == mFooterView){
                return;
            }
        }
    }

    //返回View中Item的个数，这个时候，总的个数应该是ListView中Item的个数加上HeaderView和FooterView
    @Override
    public int getItemCount() {
        if(mHeaderView == null && mFooterView == null){
            return datas.getItemses().size();
        }else if(mHeaderView == null && mFooterView != null){
            Log.i("============", "getItemCount: "+ datas.getItemses().size());
            return  datas.getItemses().size()+ 1;
        }else if (mHeaderView != null && mFooterView == null){
            return datas.getItemses().size() + 1;
        }else {
            return datas.getItemses().size()+ 2;
        }
    }

}
